package com.lhgray.sglhome.presentation.ui.compose

import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.AccountBox
import androidx.compose.material.icons.filled.Home
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import androidx.lifecycle.viewmodel.compose.viewModel
import com.lhgray.sglhome.R
import com.lhgray.sglhome.domain.model.UserData
import com.lhgray.sglhome.presentation.viewModel.HomeIntent
import com.lhgray.sglhome.presentation.viewModel.HomeViewModel
import com.lhgray.sglhome.presentation.viewModel.MyAccountViewModel

/**
 * 我的页面
 */
@Composable
fun MyAccountPage(
    homeViewModel: HomeViewModel,
    onScanBarcode: () -> Unit,//跳转到扫一扫的页面的
    viewModel: MyAccountViewModel = hiltViewModel()
) {
    val state by viewModel.uiState.collectAsStateWithLifecycle()
    val userData by homeViewModel.userData.collectAsState()
    MyAccountCompose(homeViewModel::handleIntent, userData)
}

@Composable
fun MyAccountCompose(intent: (HomeIntent) -> Unit, userData: UserData) {
    ProfileScreen(
        Modifier
            .background(color = Color.White)
            .fillMaxSize(), userData, {intent(HomeIntent.Logout)
        }, {})

}

@Composable
fun ProfileScreen(
    modifier: Modifier,
    serData: UserData,
    onLogoutClick: () -> Unit,
    onFeatureClick: (featureName: String) -> Unit
) {
    Column(
        modifier = modifier
            .fillMaxSize()
            .padding(16.dp),
        horizontalAlignment = Alignment.CenterHorizontally
    ) {
        // 用户头像
        Image(
            painter = painterResource(id = R.drawable.default_avatar), // 替换你的头像资源
            contentDescription = "用户头像",
            modifier = Modifier
                .size(120.dp)
                .clip(CircleShape)
                .border(2.dp, MaterialTheme.colorScheme.primary, CircleShape)
        )

        Spacer(modifier = Modifier.height(24.dp))

        // 用户名
        Text(
            text = serData.userName,
            style = MaterialTheme.typography.titleLarge,
            color = MaterialTheme.colorScheme.onSurface
        )

        Spacer(modifier = Modifier.height(32.dp))

        // 功能入口
        FeatureItem(
            icon = Icons.Default.Home,
            text = "Scan",
            onClick = { onFeatureClick("Home") }
        )

//        HorizontalDivider(modifier = Modifier.padding(horizontal = 16.dp))
//
//        FeatureItem(
//            icon = Icons.Default.AccountBox,
//            text = "隐私政策",
//            onClick = { onFeatureClick("privacy") }
//        )
//
//        HorizontalDivider(modifier = Modifier.padding(horizontal = 16.dp))
//
//        FeatureItem(
//            icon = Icons.Default.Done,
//            text = "帮助中心",
//            onClick = { onFeatureClick("help") }
//        )

        Spacer(modifier = Modifier.weight(1f))

        // 退出登录按钮
        Button(
            onClick = onLogoutClick,
            colors = ButtonDefaults.buttonColors(
                containerColor = MaterialTheme.colorScheme.errorContainer,
                contentColor = MaterialTheme.colorScheme.error
            ),
            modifier = Modifier.fillMaxWidth()
        ) {
            Text(text = "Log out", modifier = Modifier.padding(vertical = 8.dp))
        }
    }
}

@Composable
fun FeatureItem(
    icon: ImageVector,
    text: String,
    onClick: () -> Unit
) {
    Row(
        modifier = Modifier
            .fillMaxWidth()
            .clickable(onClick = onClick)
            .padding(vertical = 16.dp, horizontal = 24.dp),
        verticalAlignment = Alignment.CenterVertically
    ) {
        Icon(
            imageVector = icon,
            contentDescription = null,
            tint = MaterialTheme.colorScheme.primary,
            modifier = Modifier.size(24.dp)
        )

        Spacer(modifier = Modifier.width(16.dp))

        Text(
            text = text,
            style = MaterialTheme.typography.bodyLarge,
            color = MaterialTheme.colorScheme.onSurface,
            modifier = Modifier.weight(1f)
        )

        Icon(
            imageVector = Icons.Default.AccountBox,
            contentDescription = "更多",
            tint = MaterialTheme.colorScheme.onSurfaceVariant,
            modifier = Modifier.size(16.dp)
        )
    }
}

@Preview(showBackground = true)
@Composable
fun MyAccountPreviewView() {

}
